package sg.edu.nus.iss.cats.controller;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

import sg.edu.nus.iss.cats.model.User;
import sg.edu.nus.iss.cats.dataaccess.DAOException;

public class AdminServlet extends HttpServlet {

    

	public void doGet(HttpServletRequest request,HttpServletResponse response)
            throws ServletException, IOException {
              
       HttpSession userSession = request.getSession(false);
       if (userSession == null || userSession.getAttribute("USERSESSION") == null){       
            //Display login form
            HTMLUtil.displayPageHeader(request,response);
            HTMLUtil.displayLoginForm(request,response);
            HTMLUtil.displayPageFooter(request,response);
            return;
       
       }else {
       
            doPost(request,response);
       }
                   
    }
  
  
    public void doPost(HttpServletRequest request,HttpServletResponse response)
            throws ServletException, IOException {
                 
                               
        String pathInfo = request.getPathInfo();

        System.out.println("AdminServlet: " + pathInfo);
        RequestDispatcher rd = null;
        if (pathInfo == null || pathInfo.equals("/view")) {
           //By default show all user details page
           // Find all users, store in request scope and 
           // forward to the JSP that displays the view
           User[] users = null;
           try {
              //Get all users details from DB and display
              UserManager userManager = new UserManager();
              users = userManager.findAllUsers();
           }catch (DAOException e){
              HTMLUtil.displayException(request,response,
                "Sorry, an error occurred getting the users details");
              return;
           }              


          request.setAttribute("users",users);  
       }
       
        
          // Check if user has admin role then forward to modifyRoles page         
          rd = getServletContext().getRequestDispatcher( 
                                "/viewUsers.jsp");
                                
          if (pathInfo.equalsIgnoreCase("/edit")){
                rd = getServletContext().getRequestDispatcher( 
                                 "/editUsers.jsp");
          }else if (pathInfo.equalsIgnoreCase("/modify")){
                rd = getServletContext().getRequestDispatcher( 
                               "/modifyUsers.jsp");
          }else if (pathInfo.equalsIgnoreCase("/displayDept")){           
                rd = getServletContext().getRequestDispatcher( 
                                 "/displayDepartments.jsp");
          }else if (pathInfo.equalsIgnoreCase("/modifyDept")){            
                rd = getServletContext().getRequestDispatcher( 
                             "/modifyDepartments.jsp");
          }else if (pathInfo.equalsIgnoreCase("/showModifyDept")){            
                rd = getServletContext().getRequestDispatcher( 
                             "/modifyDepartmentForm.jsp");
          }else if (pathInfo.equalsIgnoreCase("/addDept")){           
                rd = getServletContext().getRequestDispatcher( 
                             "/addDepartments.jsp");
          }else if (pathInfo.equalsIgnoreCase("/showAddDept")){           
                rd = getServletContext().getRequestDispatcher( 
                             "/addDepartmentForm.jsp");
          }else if (pathInfo.equalsIgnoreCase("/deleteDept")){         
                rd = getServletContext().getRequestDispatcher( 
                                 "/deleteDepartments.jsp");
          }else{
                rd = getServletContext().getRequestDispatcher( 
                                                 "/viewUsers.jsp");
                                 
          }
        
        if (rd != null){
           rd.forward(request,response);
        }


    }
    
        

}
